﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class AdminManger_AddtuiHuo : System.Web.UI.Page
{
    SqlHelper data = new SqlHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetTypes();
            getBooks();
            GetGongsi();
        }
    }
    private void GetTypes()
    {
        ddlGType.DataSource = data.GetDataReader("select * from Bookstype ");
        ddlGType.DataTextField = "name";
        ddlGType.DataValueField = "id";
        ddlGType.DataBind();

    }
    private void GetGongsi()
    {
        DropDownList3.DataSource = data.GetDataReader("select * from Suppliers ");
        DropDownList3.DataTextField = "SName";
        DropDownList3.DataValueField = "id";
        DropDownList3.DataBind();

    }

    private void getBooks()
    {
        DropDownList1.DataSource = data.GetDataReader("select * from Books where BooksTypeID=" + ddlGType.SelectedValue);
        DropDownList1.DataTextField = "BooksName";
        DropDownList1.DataValueField = "BooksID";
        DropDownList1.DataBind();

    }
    protected void ddlGType_SelectedIndexChanged(object sender, EventArgs e)
    {
        getBooks();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            if (float.Parse(TextBox1.Text) > float.Parse(Label3.Text))
            {
                Label1.Text = "退货数量不能大于从该供应商进货的数量";
                return;
            }
            if (float.Parse(TextBox1.Text) > float.Parse(Label2.Text))
            {
                Label1.Text = "退货数量不能大于当前库存总数量";
                return;
            }
            else
            {

                data.RunSql("update  Books set BooksNum=BooksNum-" + TextBox1.Text + "where BooksID=" + DropDownList1.SelectedValue);
                data.RunSql("insert into TuiHuo(BooksId,TypeId,shuliang,GongSi)values('" + DropDownList1.SelectedValue + "','" + ddlGType.SelectedValue + "','" + TextBox1.Text + "','" + DropDownList3.SelectedItem.Text + "')");
                Label1.Text = " 退货成功";
                TextBox1.Text = "0";
            }
        }
        catch
        {
            Label2.Text = "操作错误请重新操作。。";
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataReader dr = data.GetDataReader("select * from Books where BooksID=" + DropDownList1.SelectedValue);
        if (dr.Read())
        {
            Label2.Text = dr["BooksNum"].ToString();
        }
        else
        {
            Label2.Text = "0";
        }
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataReader dr = data.GetDataReader("select sum(shuliang) as zongshuliang from jinhuo where  GongSi='" + DropDownList3.SelectedItem.Text + "' and  BooksId=" + DropDownList1.SelectedValue);
        if (dr.Read())
        {
            Label3.Text = dr["zongshuliang"].ToString();
        }
        else
        {
            Label3.Text = "0";
        }
    }

}
